using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using IGGCore;

namespace IGGConsole
{
    public class ModeDailyDugoutFormatter : IModeFormatter
    {
        private CultureInfo encaCulture = new CultureInfo("en-ca");
        #region IModeFormatter Members

        public void FormatMode(Scoreboard board)
        {
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" THE DAILY DUGOUT (" + DateTime.Now.ToString("MMMM d, yyyy", encaCulture) + " Edition)");
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[0].Title.ToUpper());
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[1].Title);
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[2].Title);
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[3].Title);
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[4].Title);
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[5].Title);
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" ");
            Console.WriteLine("                                                                       Page 1");
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[0].Title.ToUpper());
            StringFormatter.FormatLongText(board.Newspaper.Headlines[0].Content.ToLower());
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[1].Title.ToUpper());
            StringFormatter.FormatLongText(board.Newspaper.Headlines[1].Content.ToLower());
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[2].Title.ToUpper());
            StringFormatter.FormatLongText(board.Newspaper.Headlines[2].Content.ToLower());
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Headlines[3].Title.ToUpper());
            StringFormatter.FormatLongText(board.Newspaper.Headlines[3].Content.ToLower());
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" ");
            Console.WriteLine("                                                                       Page 2");
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" OPINION : ");
            Console.WriteLine(" ");
            FormatOpinion(board.Newspaper.Opinion.Content);
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" ");
            Console.WriteLine("                                                                       Page 3");
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            FormatStandings(board);
            Console.WriteLine(" ");
            Console.WriteLine(board.Statistics.DailyAVG);
            Console.WriteLine(board.Statistics.DailyHR);
            Console.WriteLine(board.Statistics.DailyRBI);
            Console.WriteLine(board.Statistics.DailyERA);
            Console.WriteLine(board.Statistics.DailyWonLost);
            FormatStat(board.NlStats.Stats["Batting Average"], board.AlStats.Stats["Batting Average"]);
            FormatStat(board.NlStats.Stats["Home Runs"], board.AlStats.Stats["Home Runs"]);
            FormatStat(board.NlStats.Stats["Runs batted in"], board.AlStats.Stats["Runs batted in"]);
            FormatStat(board.NlStats.Stats["Earned run average"], board.AlStats.Stats["Earned run average"]);
            FormatStat(board.NlStats.Stats["Wins"], board.AlStats.Stats["Wins"]);
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" ");
            Console.WriteLine("                                                                       Page 4");
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" QUICK HITS : ");
            Console.WriteLine(" ");
            Console.WriteLine(" " + board.Newspaper.Quickies[0].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[1].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[2].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[3].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[4].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[5].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[6].Title);
            Console.WriteLine(" " + board.Newspaper.Quickies[7].Title);
            Console.WriteLine(" ");
            Console.WriteLine(" ");
            Console.WriteLine(" ----------------------------------------------------------------------------");
            Console.WriteLine(" ");
            Console.WriteLine("                                                                       Page 5");
            Console.CursorLeft = 0;
            Console.CursorTop = 0;
        }

        private void FormatStat(Stat nlStat, Stat alStat)
        {
            if (nlStat != null && alStat != null)
            {
                Console.WriteLine(" " + ToolBox.GetFormattedStringPadRight("NL " + nlStat.Name.ToUpper(), 29)
                    + "AL " + alStat.Name.ToUpper());
                int minLen = alStat.SingleStats.Count;
                if (nlStat.SingleStats.Count < minLen)
                {
                    minLen = nlStat.SingleStats.Count;
                }
                if (minLen > 3)
                {
                    minLen = 3;
                }
                for (int i = 0; i < minLen; i++)
                {
                    Console.WriteLine(" "
                        + ToolBox.GetFormattedStringPadRight(nlStat.SingleStats[i].Name
                        + ", " + nlStat.SingleStats[i].Team, 18)
                        + ToolBox.GetFormattedStringPadRight(nlStat.SingleStats[i].Value, 11)
                        + ToolBox.GetFormattedStringPadRight(alStat.SingleStats[i].Name
                        + ", " + alStat.SingleStats[i].Team, 18)
                        + alStat.SingleStats[i].Value);
                }
            }
            else
            {
                Console.WriteLine(" Stats missing from current provider");
            }
            Console.WriteLine();
        }

        private void FormatOpinion(string content)
        {
            string[] parts = content.Split(new string[]{"\r\n\r\n"}, StringSplitOptions.None);
            foreach (string part in parts)
            {
                StringFormatter.FormatLongText(part);
                Console.WriteLine();
            }
        }

        public void FormatStandings(Scoreboard board)
        {
            Console.WriteLine(" NL EAST             NL CENTRAL          NL WEST             NL WILCARD:");
            Console.Write(" ");
            FormatTeamStanding(board.StandingsNLE[0], false);
            FormatTeamStanding(board.StandingsNLC[0], false);
            FormatTeamStanding(board.StandingsNLW[0], false);
            FormatTeamStanding(board.StandingsNLWC[0], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsNLE[1], false);
            FormatTeamStanding(board.StandingsNLC[1], false);
            FormatTeamStanding(board.StandingsNLW[1], false);
            FormatTeamStanding(board.StandingsNLWC[1], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsNLE[2], false);
            FormatTeamStanding(board.StandingsNLC[2], false);
            FormatTeamStanding(board.StandingsNLW[2], false);
            FormatTeamStanding(board.StandingsNLWC[2], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsNLE[3], false);
            FormatTeamStanding(board.StandingsNLC[3], false);
            FormatTeamStanding(board.StandingsNLW[3], false);
            FormatTeamStanding(board.StandingsNLWC[3], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsNLE[4], false);
            FormatTeamStanding(board.StandingsNLC[4], false);
            FormatTeamStanding(board.StandingsNLW[4], false);
            FormatTeamStanding(board.StandingsNLWC[4], true);
            Console.Write(" ");
            FormatTeamStanding(null, false);
            FormatTeamStanding(board.StandingsNLC[5], false);
            FormatTeamStanding(null, false);
            FormatTeamStanding(board.StandingsNLWC[5], true);

            Console.WriteLine();
            Console.WriteLine(" AL EAST             AL CENTRAL          AL WEST             AL WILCARD");
            Console.Write(" ");
            FormatTeamStanding(board.StandingsALE[0], false);
            FormatTeamStanding(board.StandingsALC[0], false);
            FormatTeamStanding(board.StandingsALW[0], false);
            FormatTeamStanding(board.StandingsALWC[0], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsALE[1], false);
            FormatTeamStanding(board.StandingsALC[1], false);
            FormatTeamStanding(board.StandingsALW[1], false);
            FormatTeamStanding(board.StandingsALWC[1], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsALE[2], false);
            FormatTeamStanding(board.StandingsALC[2], false);
            FormatTeamStanding(board.StandingsALW[2], false);
            FormatTeamStanding(board.StandingsALWC[2], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsALE[3], false);
            FormatTeamStanding(board.StandingsALC[3], false);
            FormatTeamStanding(board.StandingsALW[3], false);
            FormatTeamStanding(board.StandingsALWC[3], true);
            Console.Write(" ");
            FormatTeamStanding(board.StandingsALE[4], false);
            FormatTeamStanding(board.StandingsALC[4], false);
            FormatTeamStanding(null, false);
            FormatTeamStanding(board.StandingsALWC[4], true);
        }

        private void FormatTeamStanding(StandingTeam team, bool end)
        {
            if (team != null)
            {
                Console.Write(ToolBox.GetFormattedStringPadRight(team.code, 5));
                Console.Write(ToolBox.GetFormattedStringPadRight(team.w.ToString(), 4));
                Console.Write(ToolBox.GetFormattedStringPadRight(team.l.ToString(), 4));
                if (end)
                {
                    Console.WriteLine(team.gb.ToString());
                }
                else
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(team.gb.ToString(), 7));
                }
            }
            else
            {
                if (end)
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(" ", 20));
                }
            }
        }

        #endregion
    }
}
